package com.meiyu.live.tv.widget;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.R;
import com.meiyu.live.popj.RoomDanmuMessage;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by zbl on 2016/9/1.
 * 显示弹幕的view
 */
public class DanmuShowView extends FrameLayout {

    private static final int ROW_COUNT = 2;
    private static final long ANIMATION_DURATION = 10000;

    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private AnimationRoomDanmuMessage[] animationRoomDanmuMessages = new AnimationRoomDanmuMessage[ROW_COUNT];
    private Queue<RoomDanmuMessage> messageQueue = new LinkedList<>();

    class AnimationRoomDanmuMessage {
        private View anim_view;
        private ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
        private float currentX = 0;
        private int index = 0;
        public boolean canFollow = false;

        public AnimationRoomDanmuMessage(View view) {
            this.anim_view = view;
            index = (int) anim_view.getTag();
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    long currentPlayTime = valueAnimator.getCurrentPlayTime();
                    int danmuViewWidth = DanmuShowView.this.getWidth();
                    currentX = danmuViewWidth - currentPlayTime * (danmuViewWidth + anim_view.getWidth()) / ANIMATION_DURATION;
                    anim_view.setX(currentX);
                    anim_view.setY(getHeight() - (index + 1) * anim_view.getHeight());

                    boolean calcCanFollow = currentX + anim_view.getWidth() < danmuViewWidth;
                    boolean saveCanFollow = canFollow;
                    canFollow = calcCanFollow;
                    if (!saveCanFollow && calcCanFollow) {
                        RoomDanmuMessage message = messageQueue.poll();
                        if (message != null) {
                            addRoomDanmuMessage(message);
                        }
                    }
                }
            });
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    if (animationRoomDanmuMessages[index] == AnimationRoomDanmuMessage.this) {
                        animationRoomDanmuMessages[index] = null;
                    }
                    removeView(anim_view);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });
            animator.setDuration(ANIMATION_DURATION);
        }

        public void start() {
            animator.start();
        }
    }

    public DanmuShowView(Context context) {
        super(context);
        init(context);
    }

    public DanmuShowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
        inflater = LayoutInflater.from(context);
    }

    public void addRoomDanmuMessage(RoomDanmuMessage danmuMessage) {
        if (danmuMessage == null) {
            return;
        }

        for (int i = 0; i < ROW_COUNT; i++) {
            if (animationRoomDanmuMessages[i] == null) {//空栏位
                final DanmuView danmuView = new DanmuView(getContext());
                danmuView.setImageLoader(imageLoader, options);
                danmuView.setData(danmuMessage);
                danmuView.setTag(i);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                addView(danmuView, params);
                animationRoomDanmuMessages[i] = new AnimationRoomDanmuMessage(danmuView);
                animationRoomDanmuMessages[i].start();
                return;
            } else if (animationRoomDanmuMessages[i].canFollow) {//栏位有弹幕，但是可以跟随
                final DanmuView danmuView = new DanmuView(getContext());
                danmuView.setImageLoader(imageLoader, options);
                danmuView.setData(danmuMessage);
                danmuView.setTag(i);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                addView(danmuView, params);
                animationRoomDanmuMessages[i] = new AnimationRoomDanmuMessage(danmuView);
                animationRoomDanmuMessages[i].start();
                return;
            }
        }
        messageQueue.offer(danmuMessage);
    }
}
